/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package map.file;

import image.ResourceImage;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

import figures.Figure;
import figures.basic.ImageFigure;
import figures.basic.NameManager;
import figures.basic.PoliLine;
import figures.basic.Poligon;
import figures.cartography.Avenue;
import figures.cartography.Bloq;
import figures.cartography.Building;
import figures.cartography.Limit;
import figures.cartography.River;
import figures.cartography.SpeedWay;
import figures.cartography.Square;
import figures.cartography.Street;
import figures.cartography.Water;
import figures.images.Bar;
import figures.images.GasStation;
import figures.images.PostOffice;
import figures.images.Restaurant;
import figures.images.SubwayStation;
import figures.images.WebIcon;
import file.HomeGetter;

public class FigureLoader {
//	Figure implementation
	public static Figure load(InputStream is, NameManager nm) throws IOException{
		Figure f=null;	
		f = getInstance(is, nm);
		return f;
	}

	private static Figure getInstance(InputStream is, NameManager nm) throws IOException {
		int i = is.read();
		if(i<0)
			throw new EOFException();
		switch((byte)i){
			//BASIC
			case FileConstants.FIG_TYPE_PGON:return loadPolilyne(is,new Poligon(nm));
			case FileConstants.FIG_TYPE_POLI:return loadPolilyne(is,new PoliLine(nm));
			//CARTO
			case FileConstants.FIG_TYPE_STRT:return loadPolilyne(is,new Street(nm));
			case FileConstants.FIG_TYPE_AVEN:return loadPolilyne(is,new Avenue(nm));
			case FileConstants.FIG_TYPE_SPWY:return loadPolilyne(is,new SpeedWay(nm));
			case FileConstants.FIG_TYPE_SQAR:return loadPolilyne(is,new Square(nm));
			case FileConstants.FIG_TYPE_WATR:return loadPolilyne(is,new Water(nm));
			case FileConstants.FIG_TYPE_BUIL:return loadPolilyne(is,new Building(nm));
			case FileConstants.FIG_TYPE_RIVR:return loadPolilyne(is,new River(nm));	
			case FileConstants.FIG_TYPE_LIMI:return loadPolilyne(is,new Limit(nm));
			case FileConstants.FIG_TYPE_BLOQ:return loadPolilyne(is,new Bloq(nm));
			case FileConstants.FIG_TYPE_SWST:return loadImageFigure(is,new SubwayStation());
			case FileConstants.FIG_TYPE_POFF:return loadImageFigure(is,new PostOffice());
			case FileConstants.FIG_TYPE_REST:return loadImageFigure(is,new Restaurant());
			case FileConstants.FIG_TYPE_BAR:return loadImageFigure(is,new Bar());
			case FileConstants.FIG_TYPE_GASS:return loadImageFigure(is,new GasStation());
			case FileConstants.FIG_TYPE_IWEB:return loadWebIcon(is,new WebIcon());
		}
		return null;
	}
	
	static int i;
	static byte[] buffer=new byte[16];
	public static PoliLine loadPolilyne(InputStream is, PoliLine p) throws IOException {
		is.read(buffer, 0, 16);
		p.namepos = FileMap.byteArrayToInt(buffer, 0);
		p.ix = FileMap.byteArrayToInt(buffer, 4);
		p.iy = FileMap.byteArrayToInt(buffer, 8);
		i = FileMap.byteArrayToInt(buffer, 12);
		p.l = i & 0xFFFF;
		p.offsetinit = i>>16;
		p.prepareBoundingBox();
		return p;
	}
	
	public static ImageFigure loadImageFigure(InputStream is, ImageFigure i) throws IOException {
		i.n = FileMap.readString(is).getBytes();
		is.read(buffer, 0, 8);
		i.setPos(FileMap.byteArrayToInt(buffer, 0), FileMap.byteArrayToInt(buffer, 4));
		return i;
	}
	public static ImageFigure loadWebIcon(InputStream is, ImageFigure i) throws IOException {
		i.n = FileMap.readString(is).getBytes();
		is.read(buffer, 0, 8);
		int x=FileMap.byteArrayToInt(buffer, 0);
		int y=FileMap.byteArrayToInt(buffer, 4);
		is.read(buffer, 0, 4);
		i.setImage(new ResourceImage(HomeGetter.getInstance(), FileMap.byteArrayToInt(buffer, 0)));
		i.setPos(x,y);
		return i;
	}
}
